﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Beacon.SPC;

namespace Beacon.Test
{
    public class TrainTester
    {
        private static double[] rawData = new double[] { 
                1.3235,1.4128,1.6744,1.4573,1.6914,
                1.4314,1.3592,1.6075,1.4666,1.6109,
                1.4284,1.4871,1.4932,1.4324,1.5674,
                1.5028,1.6352,1.3841,1.2831,1.5507,
                1.5604,1.2735,1.5265,1.4363,1.6441,
                1.5955,1.5451,1.3574,1.3281,1.4198,
                1.6274,1.5064,1.8366,1.4177,1.5144,
                1.4190,1.4303,1.6637,1.6067,1.5519,
                1.3884,1.7277,1.5355,1.5176,1.3688,
                1.4039,1.6697,1.5089,1.4627,1.5220,
                1.4158,1.7667,1.4278,1.5928,1.4181,
                1.5821,1.3355,1.5777,1.3908,1.7559,
                1.2856,1.4106,1.4447,1.6398,1.1928,
                1.4951,1.4036,1.5893,1.6458,1.4969,
                1.3589,1.2863,1.5996,1.2497,1.5471,
                1.5747,1.5301,1.7171,1.8839,1.8662,
                1.3680,1.7269,1.3957,1.5014,1.4449,
                1.4163,1.3864,1.3057,1.6210,1.5573,
                1.5796,1.4185,1.6541,1.5116,1.7247,
                1.7106,1.4412,1.2361,1.3820,1.7601,
                1.5301,1.7100,1.8005,1.8633,1.6866,
                1.4371,1.5051,1.3485,1.5670,1.4880,
                1.4738,1.5936,1.6583,1.4973,1.4720,
                1.5917,1.4333,1.5551,1.5295,1.6866,
                1.6399,1.5243,1.5705,1.5563,1.5530,
                1.5797,1.3663,1.6240,1.3732,1.6887};

        public static void test()
        {
            List<SpcData> data = new List<SpcData>();
            List<SpcRuleDefinition> rules = new List<SpcRuleDefinition>();
            ScalarInspection insp = new ScalarInspection(1.700, 1.300);

            // generate data 
            int index = 0;
            SpcData spc = new SpcData();
            foreach (double d in rawData)
            {
                if (index == 0)
                {
                    spc = new SpcData();
                }

                spc.Add(d);
                index++;
                if (index == 4)
                {
                    data.Add(spc);
                    index = 0;
                }
            }

            // generate rule
            rules.Add(new SpcRuleDefinition(SpcRules.WERule1, SpcControlSide.Both));
            rules.Add(new SpcRuleDefinition(SpcRules.WERule2, SpcControlSide.Both));
            //rules.Add(new SpcRuleDefinition(SpcRules.WERule3, SpcControlSide.Both));

            SpcTraining trainer = new SpcTraining();
            SpcTrainingResult rlt;
            // XBarR
            rlt= trainer.Train(data, rules, SpcChartType.XBarR, null, true);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Train XbarR result:");
            Console.WriteLine(rlt.ToString());
            Console.WriteLine("-------------------------");

            // XBarS
            rlt = trainer.Train(data, rules, SpcChartType.XBarS, null, true);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Train XbarS result:");
            Console.WriteLine(rlt.ToString());
            Console.WriteLine("-------------------------");

            // MR
            rlt = trainer.Train(data, rules, SpcChartType.XMR, null, true);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Train XMR result:");
            Console.WriteLine(rlt.ToString());
            Console.WriteLine("-------------------------");

            // C
            rlt = trainer.Train(data, rules, SpcChartType.C, insp, true);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Train C result:");
            Console.WriteLine(rlt.ToString());
            Console.WriteLine("-------------------------");

            // U
            rlt = trainer.Train(data, rules, SpcChartType.U, insp, true);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Train U result:");
            Console.WriteLine(rlt.ToString());
            Console.WriteLine("-------------------------");

            // NP
            rlt = trainer.Train(data, rules, SpcChartType.NP, insp, true);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Train NP result:");
            Console.WriteLine(rlt.ToString());
            Console.WriteLine("-------------------------");

            // P
            rlt = trainer.Train(data, rules, SpcChartType.P, insp, true);
            Console.WriteLine("-------------------------");
            Console.WriteLine("Train P result:");
            Console.WriteLine(rlt.ToString());
            Console.WriteLine("-------------------------");

        }

    }
}
